﻿using Ivory;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using DefCore.AI;
using DefCore.Core;
using DefCore.Tools;
using DefCore.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Krypton.Lights;
using Krypton;
using DefCore.Interfaces;
using DefCore.AI.Pathing;

namespace DefCore.Structures
{
    public abstract class Structure : EntityBase, IHealthController
    {
        //Fields
        public CellBase ParentCell { get; private set; }
        public StructureFrameEnum Frame { get; private set; }
        public StructureType Type { get; private set; }

        public int CurrentHealth
        {
            get;
            private set;
        }

        private int _MaxHealth = 10;
        public int MaxHealth
        {
            get { return _MaxHealth; }
            protected set
            {
                _MaxHealth = value;
                CurrentHealth = value;
            }
        }

        public override Light2D EntityLight
        {
            get { return null; }
        }
        public override ShadowHull EntityHull
        {
            get { return null; }
        }
        public override Rectangle HitBox
        {
            get
            {
                return FrameManager.GetFrame(Frame, ParentCell);
            } 
        }

        //ctor
        public Structure(CellBase cell, StructureType type, StructureFrameEnum frame)
            : base(Objects.LevelInstance)
        {
            //Set the Hit Rectangle
            this.ParentCell = cell;
            this.Position = cell.MapPosition;
            this.Frame = frame;
            this.Type = type;            
        }
        
        //Methods
        public sealed override void Create()
        { 
            OnCreate();
            base.Create();
        }
        public sealed override void Delete() 
        { 
            OnDelete();
            Objects.LevelInstance.StructureControlUnit.Remove(this, true);
            base.Delete();
        }

        protected virtual void OnCreate() { }
        protected virtual void OnDelete() { }

        public void Damage(int amount)
        {
            CurrentHealth -= amount;

            if (CurrentHealth <= 0)
            {
                //WE DIED 
                Delete();
            }
        }
        public void Heal(int amount)
        {
            CurrentHealth = (CurrentHealth + amount) >= CurrentHealth ? CurrentHealth : (CurrentHealth + amount);
        }
    }
}
